package com.episode.duetapi.controller;

import java.util.List;

import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.episode.duetapi.exceptionHandle.CustomerException;
import com.episode.duetapi.model.Menu;
import com.episode.duetapi.service.MenuService;
import com.episode.duetapi.utils.ResultCode;
import com.episode.duetapi.utils.ResultTools;

@RestController
@RequestMapping("menu")
public class MenuController {
	
	@Autowired
	private MenuService menuService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Object getMenuList(@RequestParam("keyWord")String keyWord) {
		List<Menu> menuList = menuService.getMenuList(keyWord);	
		return ResultTools.getDataResult(menuList);
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public Object addMenu() {
		List<Menu> menuList = menuService.getParentMenu();
		return ResultTools.getDataResult(menuList);
	}
	
	@RequestMapping(value="/edit")
    public Object editMenu(@ModelAttribute Menu menu) throws CustomerException{
		menuService.editMenu(menu);
		return ResultTools.getDataResult(ResultCode.DATA_SUCCESS);
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST,consumes=MediaType.APPLICATION_FORM_URLENCODED)
	public Object addSave(Menu menu) throws CustomerException{             
		menu.setHidden(false);
		menu.setLeaf(false);
		menuService.insertMenu(menu);
		return ResultTools.getDataResult(ResultCode.DATA_SUCCESS);
	}

	@RequestMapping(value="/del",method=RequestMethod.DELETE)
	public Object delMenu(@RequestParam("id")Integer id) {
		menuService.deleteMenu(id);
		return ResultTools.getDataResult(ResultCode.DATA_SUCCESS);
	}
}
